package demo.util;

import java.security.Key;
import java.util.Date;

import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JWTUtil {
	/**
	 * token鍔犲瘑鏃朵娇鐢ㄧ殑瀵嗛挜
	 * 涓�鏃﹀緱鍒拌瀵嗛挜涔熷氨鍙互浼�爐oken浜�
	 */
	public static String sercetKey = "InMySchoolOnline";
	/**
	 * 浠ｈ〃token鐨勬湁鏁堟椂闂�
	 */
	public final static long keeptime = 1800000;
	
	/**
	 * JWT鐢�3涓儴鍒嗙粍鎴�,鍒嗗埆鏄� 澶撮儴Header,杞借嵎Payload涓�鑸槸鐢ㄦ埛淇℃伅鍜屽０鏄�,绛捐瘉Signature涓�鑸槸瀵嗛挜鍜岀鍚�
	 * 褰撳ご閮ㄧ敤base64杩涜缂栫爜鍚庝竴鑸兘浼氬憟鐜癳yJ...褰㈠紡,鑰岃浇鑽蜂负闈炲己鍒朵娇鐢�,绛捐瘉鍒欏寘鍚簡鍝堝笇绠楁硶鍔犲瘑鍚庣殑鏁版嵁,鍖呮嫭杞爜鍚庣殑header,payload鍜宻ercetKey
	 * 鑰宲ayload鍙堝寘鍚嚑涓儴鍒�,issuer绛惧彂鑰�,subject闈㈠悜鐢ㄦ埛,iat绛惧彂鏃堕棿,exp杩囨湡鏃堕棿,aud鎺ユ敹鏂广��
	 * @Title: generToken   
	 * @Description: TODO
	 * @param: @param id 鐢ㄦ埛id
	 * @param: @param issuer 绛惧彂鑰�
	 * @param: @param subject 涓�鑸敤鎴峰悕 
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String generToken(String id, String issuer, String subject) {
		long ttlMillis = keeptime;
		SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
		//浣跨敤Hash256绠楁硶杩涜鍔犲瘑
		long nowMillis = System.currentTimeMillis();
		Date now = new Date(nowMillis);
		//鑾峰彇绯荤粺鏃堕棿浠ヤ究璁剧疆token鏈夋晥鏃堕棿
		byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(sercetKey);
		//灏嗗瘑閽ヨ浆鐮佷负base64褰㈠紡,鍐嶈浆涓哄瓧鑺傜爜
		Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
		//瀵瑰叾浣跨敤Hash256杩涜鍔犲瘑
		JwtBuilder builder = Jwts.builder().setId(id).setIssuedAt(now);
		//JWT鐢熸垚绫�,姝ゆ椂璁剧疆iat,浠ュ強鏍规嵁浼犲叆鐨刬d璁剧疆token
		if (subject != null) {
			builder.setSubject(subject);
		}
		if (issuer != null) {
			builder.setIssuer(issuer);
		}
		//鐢变簬Payload鏄潪蹇呴』鍔犲叆鐨�,鎵�浠ヨ繖鏃跺�欒鍔犲叆妫�娴�
		builder.signWith(signatureAlgorithm, signingKey);
		//杩涜绛惧悕,鐢熸垚Signature
		if (ttlMillis >= 0) {
			long expMillis = nowMillis + ttlMillis;
			Date exp = new Date(expMillis);
			builder.setExpiration(exp);
		}
		//杩斿洖鏈�缁堢殑token缁撴灉
		return builder.compact();
	}
	/**
	 * 璇ュ嚱鏁扮敤浜庢洿鏂皌oken
	 * @Title: updateToken   
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public static String updateToken(String token) {
		//Claims灏辨槸鍖呭惈浜嗘垜浠殑Payload淇℃伅绫�
		Claims claims = verifyToken(token);
		String id = claims.getId();
		String subject = claims.getSubject();
		String issuer = claims.getIssuer();
		//鐢熸垚鏂扮殑token,鏍规嵁鐜板湪鐨勬椂闂�
		return generToken(id, issuer, subject);
	}
	/**
	 * 灏唗oken瑙ｅ瘑鍑烘潵,灏唒ayload淇℃伅鍖呰鎴怌laims绫昏繑鍥�
	 * @Title: verifyToken   
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return      
	 * @return: Claims      
	 * @throws
	 */
	private static Claims verifyToken(String token) {
		Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary(sercetKey))
				.parseClaimsJws(token).getBody();
		return claims;
	}
}
